import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { OrgApi } from '#/api/system/org';

import { z } from '#/adapter/form';
import { getOrgList } from '#/api/system/org';
import { $t } from '#/locales';

/**
 * 获取编辑表单的字段配置。如果没有使用多语言，可以直接export一个数组常量
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Select',
      componentProps: {
        class: 'relative flex w-full items-center',
        options: [
          { label: '集团', value: '1' },
          { label: '公司', value: '2' },
          { label: '部门', value: '3' },
        ],
        optionType: 'button',
      },
      defaultValue: '3',
      fieldName: 'otype',
      label: $t('system.org.otype'),
    },
    {
      component: 'Input',
      fieldName: 'ono',
      label: $t('system.org.ono'),
    },
    {
      component: 'Input',
      fieldName: 'oname',
      label: $t('system.org.oname'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('system.org.oname'), 2]))
        .max(20, $t('ui.formRules.maxLength', [$t('system.org.oname'), 20])),
    },
    {
      component: 'Input',
      fieldName: 'level',
      label: $t('system.org.level'),
      defaultValue: 3,
    },
    {
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: getOrgList,
        class: 'w-full',
        labelField: 'oname',
        valueField: 'ono',
        childrenField: 'children',
      },
      fieldName: 'pono',
      label: $t('system.org.parentOrg'),
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
      defaultValue: 1,
      fieldName: 'status',
      label: $t('system.org.status'),
    },
    {
      component: 'Textarea',
      componentProps: {
        class: 'relative flex w-full items-center',
        maxLength: 200,
        rows: 3,
        showCount: true,
      },
      fieldName: 'memo',
      label: $t('system.org.memo'),

      rules: z
        .string()
        .max(500, $t('ui.formRules.maxLength', [$t('system.dept.remark'), 200]))
        .optional(),
    },
  ];
}

/**
 * 获取表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<OrgApi.Org>,
): VxeTableGridOptions<OrgApi.Org>['columns'] {
  return [
    {
      align: 'left',
      field: 'oname',
      fixed: 'left',
      title: $t('system.org.oname'),
      treeNode: true,
      width: 150,
    },
    {
      align: 'left',
      field: 'ono',
      fixed: 'left',
      title: $t('system.org.ono'),
      width: 150,
    },
    {
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'error', label: '集团', value: '1' },
          { color: 'success', label: '公司', value: '2' },
          { color: 'processing', label: '部门', value: '3' },
        ],
      },

      align: 'left',
      field: 'otype',
      title: $t('system.org.otype'),
      width: 100,
    },
    {
      field: 'level',
      title: $t('system.org.level'),
      width: 100,
    },
    {
      cellRender: { name: 'CellTag' },
      field: 'status',
      title: $t('system.org.status'),
      width: 100,
    },
    {
      field: 'createdTime',
      title: $t('system.org.createdTime'),
      width: 180,
    },
    {
      field: 'memo',
      title: $t('system.dept.remark'),
    },
    {
      align: 'right',
      cellRender: {
        attrs: {
          nameField: 'oname',
          nameTitle: $t('system.org.oname'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'append',
            text: '新增',
            // icon: 'ri-add-circle-line',
          },
          //'edit', // 默认的编辑按钮
          {
            code: 'edit',
            text: '编辑',
            //icon: 'ri-edit-line',
          },
          {
            code: 'delete', // 默认的删除按钮
            text: '删除',
            //icon: 'ri-delete-bin-line',
            disabled: (row: OrgApi.Org) => {
              return !!(row.children && row.children.length > 0);
            },
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      headerAlign: 'center',
      showOverflow: false,
      title: $t('system.org.operation'),
      width: 200,
    },
  ];
}
